import { createRouter, createWebHashHistory } from 'vue-router'

export function setupRouter(app, routes) {
  const router = createRouter({
    history: createWebHashHistory(),
    routes,
    strict: true,
    scrollBehavior: () => ({ left: 0, top: 0 }),
  })

  app.use(router)

  return router
}

export const handledRoutes = Object.values(import.meta.glob('./modules/*.js', { eager: true })).map((v) => {
  return v.route
})

export function findRouteResult(keyPath, routes = []) {
  const keyPathResult = keyPath.split('/')
  const name = keyPathResult[keyPathResult.length - 1]
  const result = routes.reduce((acc, item) => {
    if (item.name === name) {
      acc = {
        ...item,
        fullPath: keyPath,
      }
    }
    // if (item.children && item.children.length) {
    //   acc = findRouteResult(keyPath, item.children)
    // }
    return acc
  }, null)
  return result
}
